import json
from fastapi import APIRouter, HTTPException
from typing import Optional, Any
from pydantic import BaseModel


# setting
from settings.settings import settings
# agv socket 工厂方法
from sockets.agvsocket import AgvSocket

agv_so_manage = AgvSocket()

# 创建status router路由
other_router = APIRouter()




class StatusParams(BaseModel):
    msg_type: int
    msg: Optional[Any]

    class Config:
        schema_extra = {
            "example": {
                "msg_type": 1000,
                "msg": {"参数名称": "xx参数"}
            }
        }


@other_router.get("/")
async def get_other(msg_type: int):
    agv_other_socket = agv_socket_factory.get(Port.OTHER.value)
    try:
        agv_other_socket.send(1, msg_type)
        res = agv_other_socket.receive()
        return res
    except Exception as e:
        print(e)
        raise HTTPException(
            status_code=409,
            detail="查询编号不正确"
        )


@other_router.post("/", response_description="编号 查询结果")
async def post_other(params: StatusParams):
    agv_other_socket = agv_socket_factory.get(Port.OTHER.value)
    try:
        agv_other_socket.send(1, params.msg_type, params.msg)
        res = agv_other_socket.receive()
        return res
    except Exception as e:
        print(e)
        raise HTTPException(
            status_code=409,
            detail="查询编号不正确"
        )
